package com.doctor.ui.consulting;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.bean.DoctorBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.FileHelper;
import com.doctor.comm.FragmentHelper;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;
import com.doctor.ui.consulting.view.ConsultingActivity;
import com.tencent.connect.auth.QQAuth;
import com.tencent.wpa.WPA;

public class DoctorDetailFrag extends BaseFragment{

	/**
	 * 医师详情
	 */
	
	private Context context;
	private TextView name,sex,hospital,postTitle,partment,post,advantage,diagTime,address,phone,mobile,
	        specialNum,email,qq,blog,fetion,webChart,webSite,other;
	private TextView birthday;	
	private DoctorBean bean;
	private QQAuth mQQAuth;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context=getActivity();
		bean=(DoctorBean) getArguments().getSerializable(ConstConfig.BEAN);
		
		View view=inflater.inflate(R.layout.doctor_detail, container, false);
		initiaView(view);
				
		return view;
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
//		context=null;
//		bean=null;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}	
	

	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		name=(TextView) view.findViewById(R.id.name);
		name.setText(bean.getRealname());
		
		sex=(TextView) view.findViewById(R.id.sex);
		String sex_txt=bean.getSex();
		if (sex_txt!=null && "男".equals(sex_txt.trim())) {
			sex.setText("男");
		} else if (sex_txt!=null && "女".equals(sex_txt.trim())) {
			sex.setText("女");
		}
		
		hospital=(TextView) view.findViewById(R.id.hospital);
		hospital.setText(bean.getHospital());
		
		postTitle=(TextView) view.findViewById(R.id.postTitle);
		postTitle.setText(bean.getZc());
		
		partment=(TextView) view.findViewById(R.id.partment);
		partment.setText(bean.getKs());
		
		post=(TextView) view.findViewById(R.id.post);
		post.setText(bean.getZw());
		
		advantage=(TextView) view.findViewById(R.id.advantage);
		if (ConsultingActivity.map1.size()>0) {
			advantage.setText(ConsultingActivity.map1.get(bean.getExpertise()));
		}
		
		diagTime=(TextView) view.findViewById(R.id.diagTime);
		diagTime.setText(bean.getCztime());
		
		address=(TextView) view.findViewById(R.id.address);
		address.setText(bean.getAddress());
		
		phone=(TextView) view.findViewById(R.id.phone);
		phone.setText(bean.getTel());
		phone.setOnClickListener(click);
		
		mobile=(TextView) view.findViewById(R.id.mobile);
		mobile.setText(bean.getMobile());
		mobile.setOnClickListener(click);
		
		specialNum=(TextView) view.findViewById(R.id.specialNum);
		specialNum.setText(bean.getZh());
		specialNum.setOnClickListener(click);
		
		email=(TextView) view.findViewById(R.id.email);
		email.setText(bean.getEmail());
		email.setOnClickListener(click);
		
		qq=(TextView) view.findViewById(R.id.qq);
		qq.setText(bean.getQq());
		qq.setOnClickListener(click);
		
		blog=(TextView) view.findViewById(R.id.blog);
		blog.setText(bean.getBlog());
		
		fetion=(TextView) view.findViewById(R.id.fetion);
		fetion.setText(bean.getFx());
		
		webChart=(TextView) view.findViewById(R.id.webChart);
		webChart.setText(bean.getWx());
		
		webSite=(TextView) view.findViewById(R.id.webSite);
		webSite.setText(bean.getWebsite());
		
		other=(TextView) view.findViewById(R.id.other);		
		other.setText(bean.getRemark());
		
		birthday=(TextView) view.findViewById(R.id.birthday);
		birthday.setText(bean.getBrith());
	}	
	
	
	View.OnClickListener click=new View.OnClickListener() {		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0==phone || arg0==mobile || arg0==specialNum) {
				//拨打电话
				String phoneNum=((TextView) arg0).getText().toString().trim();
				FileHelper.dialPhone(getActivity(), context, phoneNum,"未填写号码！");
			} else if (arg0==qq) {
				//qq咨询
				String qq_txt=qq.getText().toString().trim();
				if (qq_txt==null || "".equals(qq_txt)) {
					Toast.makeText(context, "未填写qq号！", Toast.LENGTH_SHORT).show();
					return;
				}
				boolean isQqInstalled=FileHelper.isPkgInstalled(context, "com.tencent.mobileqq");
				if (!isQqInstalled) {
					Toast.makeText(context, "尚未安装QQ，无法启动！", Toast.LENGTH_SHORT).show();
					return;
				}
				
				mQQAuth = QQAuth.createInstance("101108930", getActivity().getApplicationContext());
				WPA mWPA = new WPA(getActivity(), mQQAuth.getQQToken());
				mWPA.startWPAConversation(qq_txt, "");
			} else if (arg0==email) {
				//邮件咨询
				String email_txt=email.getText().toString().trim();
				if (email_txt==null || "".equals(email_txt)) {
					Toast.makeText(context, "未填写邮箱！", Toast.LENGTH_SHORT).show();
					return;
				}
				SendEmailFrag sendEmailFrag=new SendEmailFrag();
				Bundle bundle=new Bundle();
				bundle.putString("email", email_txt);
				sendEmailFrag.setArguments(bundle);
				FragmentTransaction transaction=getActivity().getSupportFragmentManager().beginTransaction();
				FragmentHelper.setFragSwitchAnimation(transaction);
				transaction.replace(R.id.frag_container, sendEmailFrag).addToBackStack(null).commit();
			}
		}
	};
	
	
	
	
}
